<script setup lang="ts">
import CategoryView from '@/components/Category/index.vue'
import { ref, watch, onBeforeUnmount } from 'vue'
import useCategoryStore from '@/store/modules/category'
import {
  reqAllSaleAttr,
  reqAllTradeMark,
  reqHasSpu,
  reqSkuList,
  reqRemoveSpu,
} from '@/api/product/spu/index'
import type { HasSpuResponseData, Records } from '@/api/product/spu/type'
import SpuFrom from './spuForm.vue'
import SkuFrom from './skuForm.vue'
import type { SpuData, SkuData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'
let categoryStore = useCategoryStore()
//spu的场景值
let scene = ref(0) // 0:默认显示 1：添加/修改SPU 2：添加Sku
//每一页展示的数据个数
let pageSize = ref(3)
//当前默认展示页码
let currentPage = ref(1)
let total = ref<number>(0)
//存储已有的SPU数据
let records = ref<Records>([])

//通过使用v-show在组件挂载完毕时获取子组件实例
const spu = ref<any>('spu')
const sku = ref<any>('sku')
// 存放显示的sku数据
const skuArr = ref<any>([])
// 控制对话框显示与隐藏的
let show = ref<boolean>(false)
const getHasSpu = async (pager = 1) => {
  currentPage.value = pager
  let res: HasSpuResponseData = await reqHasSpu(
    currentPage.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  // console.log(res)
  if (res.code == 200) {
    records.value = res.data.records
    total.value = res.data.total
  }
}

watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) {
      return //没有三级分类的时候不发请求
    }
    getHasSpu()
  },
)

const deleteSpu = async (row: any) => {
  let res = await reqRemoveSpu(row.id)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getHasSpu(
      records.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
const changeSize = () => {
  getHasSpu()
}
//点击按钮的场景切换/添加SPU
const addSpu = () => {
  scene.value = 1
  //调用子组件spu的页面，并初始化页面
  spu.value.initAddSpu(categoryStore.c3Id)
}
const updataSpu = (row: SpuData) => {
  //在父组件这调用子组件的方法
  spu.value.initHasSpuData(row)
  scene.value = 1
}

//子组件点击取消后的场景切换
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params == 'update') {
    getHasSpu(currentPage.value)
  } else {
    getHasSpu()
  }
}

// 添加SKU
const addSku = (row: any) => {
  scene.value = 2
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

// 查看sku
const viewSku = async (row: any) => {
  let res = await reqSkuList(row.id)
  // console.log(res)
  if (res.code == 200) {
    skuArr.value = res.data
    show.value = true
  }
}

onBeforeUnmount(() => {
  // console.log(CategoryStore)
  categoryStore.$reset()
})
</script>

<template>
  <div>
    <CategoryView :scene="scene"></CategoryView>
    <!-- 场景1，默认场景 -->
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                title="添加SKU"
                icon="Plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                title="修改SPU"
                @click="updataSpu(row)"
                icon="Edit"
              ></el-button>
              <el-button
                type="primary"
                title="查看SPU"
                icon="View"
                @click="viewSku(row)"
              ></el-button>
              <el-popconfirm
                :title="`你确定删除${row.spuName}?`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[2, 4, 6, 8]"
          background
          layout="prev, pager, next, jumper, -> ,total, sizes"
          :total="total"
          @current-change="getHasSpu(currentPage)"
          @size-change="changeSize"
        />
      </div>
      <!-- 显示sku的对话框 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <!-- 场景2添加/修改SPU场景 -->
      <SpuFrom
        ref="spu"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuFrom>
      <SkuFrom
        ref="sku"
        v-show="scene == 2"
        @changeScene="changeScene"
      ></SkuFrom>
    </el-card>
  </div>
</template>

<style scoped></style>
